{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting with Folium\n",
    "\n",
    "__What is Folium?__\n",
    "\n",
    "[Folium](https://python-visualization.github.io/folium/) builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the leaflet.js library. This allows you to manipulate your data in Geopandas and visualize it on a Leaflet map via Folium.\n",
    "\n",
    "In this example, we will first use Geopandas to load the geometries (volcano point data), and then create the Folium map with markers representing the different types of volcanoes."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load geometries\n",
    "This example uses a freely available [volcano dataset](https://www.kaggle.com/texasdave/volcano-eruptions). We will be reading the csv file using pandas, and then convert the pandas `DataFrame` to a Geopandas `GeoDataFrame`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import Libraries\n",
    "import pandas as pd\n",
    "import geopandas\n",
    "import folium\n",
    "import geodatasets\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = pd.read_csv(\"volcano_data_2010.csv\")\n",
    "\n",
    "# Keep only relevant columns\n",
    "df = df1.loc[:, (\"Year\", \"Name\", \"Country\", \"Latitude\", \"Longitude\", \"Type\")]\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create point geometries\n",
    "geometry = geopandas.points_from_xy(df.Longitude, df.Latitude)\n",
    "geo_df = geopandas.GeoDataFrame(\n",
    "    df[[\"Year\", \"Name\", \"Country\", \"Latitude\", \"Longitude\", \"Type\"]], geometry=geometry\n",
    ")\n",
    "\n",
    "geo_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "world = geopandas.read_file(geodatasets.get_path(\"naturalearth.land\"))\n",
    "df.Type.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "nbsphinx-thumbnail"
    ]
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(24, 18))\n",
    "world.plot(ax=ax, alpha=0.4, color=\"grey\")\n",
    "geo_df.plot(column=\"Type\", ax=ax, legend=True)\n",
    "plt.title(\"Volcanoes\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Folium map\n",
    "Folium has a number of built-in tilesets from OpenStreetMap, Mapbox, and CartoDB. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# CartoDB Positron\n",
    "map = folium.Map(location=[13.406, 80.110], tiles=\"CartoDB Positron\", zoom_start=9)\n",
    "map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# OpenStreetMap\n",
    "map = folium.Map(location=[13.406, 80.110], tiles=\"OpenStreetMap\", zoom_start=9)\n",
    "map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add markers\n",
    "To represent the different types of volcanoes, you can create Folium markers and add them to your map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a geometry list from the GeoDataFrame\n",
    "geo_df_list = [[point.xy[1][0], point.xy[0][0]] for point in geo_df.geometry]\n",
    "\n",
    "# Iterate through list and add a marker for each volcano, color-coded by its type.\n",
    "i = 0\n",
    "for coordinates in geo_df_list:\n",
    "    # assign a color marker for the type of volcano, Strato being the most common\n",
    "    if geo_df.Type[i] == \"Stratovolcano\":\n",
    "        type_color = \"green\"\n",
    "    elif geo_df.Type[i] == \"Complex volcano\":\n",
    "        type_color = \"blue\"\n",
    "    elif geo_df.Type[i] == \"Shield volcano\":\n",
    "        type_color = \"orange\"\n",
    "    elif geo_df.Type[i] == \"Lava dome\":\n",
    "        type_color = \"pink\"\n",
    "    else:\n",
    "        type_color = \"purple\"\n",
    "\n",
    "    # Place the markers with the popup labels and data\n",
    "    map.add_child(\n",
    "        folium.Marker(\n",
    "            location=coordinates,\n",
    "            popup=\"Year: \"\n",
    "            + str(geo_df.Year[i])\n",
    "            + \"<br>\"\n",
    "            + \"Name: \"\n",
    "            + str(geo_df.Name[i])\n",
    "            + \"<br>\"\n",
    "            + \"Country: \"\n",
    "            + str(geo_df.Country[i])\n",
    "            + \"<br>\"\n",
    "            + \"Type: \"\n",
    "            + str(geo_df.Type[i])\n",
    "            + \"<br>\"\n",
    "            + \"Coordinates: \"\n",
    "            + str(geo_df_list[i]),\n",
    "            icon=folium.Icon(color=\"%s\" % type_color),\n",
    "        )\n",
    "    )\n",
    "    i = i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Folium Heatmaps\n",
    "\n",
    "Folium is well known for its heatmaps, which create a heatmap layer. To plot a heatmap in Folium, you need a list of latitudes and longitudes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This example uses heatmaps to visualize the density of volcanoes\n",
    "# which is more in some parts of the world compared to others.\n",
    "\n",
    "from folium import plugins\n",
    "\n",
    "map = folium.Map(location=[15, 30], tiles=\"Cartodb dark_matter\", zoom_start=2)\n",
    "\n",
    "heat_data = [[point.xy[1][0], point.xy[0][0]] for point in geo_df.geometry]\n",
    "\n",
    "heat_data\n",
    "plugins.HeatMap(heat_data).add_to(map)\n",
    "\n",
    "map"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
